package com.waves.biz;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.concurrent.atomic.AtomicInteger;

@RestController
public class UserController {
    private UserServiceImpl userService;

    @Autowired
    public UserController(UserServiceImpl userService) {
        this.userService = userService;
    }

    private final AtomicInteger idGenerator = new AtomicInteger();

    @PostMapping("/user")
    public User add(String name) {
        User param = new User().setId(idGenerator.incrementAndGet()).setName(name);
        User result = userService.add(param);
        System.out.printf("用户对象：%s 保存成功 \n", result);  // 用户对象：User(id=1, name=张三) 保存成功
        return result;
    }

    @GetMapping("/user/{id}")
    public User getById(@PathVariable("id") Integer id) {
        User user = userService.getUserById(id);
        return user;
    }

    @PutMapping(value = "/user")
    public User update(User param) {
        return userService.update(param);
    }

    @DeleteMapping(value = "/user/{id}")
    public void delete(@PathVariable Integer id) {
        userService.remove(id);
    }
}
